using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Enyo
{
	public partial class RenderingControl
	{
		private DataDecorator m_Decorator;
		[Category("Data")]
		public DataDecorator Decorator
		{
			get { return m_Decorator; }
			set
			{
				if (m_Decorator != value)
				{
					if (m_Decorator != null)
					{
					}
					m_Decorator = value;
					if (m_Decorator != null)
					{
					}
				}
			}
		}


		#region Value

		private object m_Value;
		[Category("Data")]
		/// <summary>
		/// The value to be rendered.
		/// </summary>
		public object Value
		{
			get { return m_Value; }
			set
			{
				if (m_Value != value)
				{
					this.OnValueChanged(m_Value, m_Value = value);
				}
			}
		}

		#region ValueChanged event
		public delegate void ValueChangedHandler(object sender, object oldvalue, object newvalue);

		private event ValueChangedHandler m_ValueChanged;
		/// <summary>
		/// Occurs when Value changes.
		/// </summary>
		public event ValueChangedHandler ValueChanged
		{
			add { this.m_ValueChanged += value; }
			remove { this.m_ValueChanged -= value; }
		}

		/// <summary>
		/// Raises the ValueChanged event.
		/// </summary>
		protected virtual void OnValueChanged(object oldvalue, object newvalue)
		{
			this.Decorator = this.CreateDecorator(newvalue);
			// if there are event subscribers...
			if (this.m_ValueChanged != null)
			{
				// call them...
				this.m_ValueChanged(this, oldvalue, newvalue);
			}
		}
		#endregion ValueChanged event
		#endregion Value

		public DataDecorator CreateDecorator(object value)
		{
			DataDecorator decorator = null;
			//Type t = value.GetType();
			//Type td = typeof(DataDecorator<>);
			//td = td.MakeGenericType(t);
			//decorator = Activator.CreateInstance(td) as DataDecorator;
			decorator = Enyo.Reflection.Type<DataDecorator>.CreateInstance(typeof(DataDecorator<>), value.GetType());

			return decorator;
		}

		public RenderingControl()
		{
#if WINFORMS
			InitializeComponent();
#endif
		}
	}
}
